string URL   = "http://omtech-n2k.appspot.com/?name=";
string KEY   = "";
string NAME   = "";
key reqid;
list text;
string command;
string tempname;
string object = "Beam";
integer goto;
vector Pos;
vector getpos;
float cordx;
float cordy;
float cordz;
vector relativePosOffset = <1.0, 0.0, 0.2>;
rotation relativeRot = <0.0, 0.0, 0.0, 0.0>;
integer startParam = 10;
list people_in_sensor_range;

default
{
    state_entry()
    {
        llListen(0 ,"",llGetOwner(),"");
        llSensorRepeat("", "", AGENT, 99.0, PI, 3.0);
    }
    
    on_rez(integer start_param)
    {
        llResetScript(); 
    }
    
    changed(integer change)
    {
        if (change & CHANGED_TELEPORT)
        {
            people_in_sensor_range = [];
        }
    }
    
    attach(key id)
    {
        if(id)//tests if it is a valid key and not NULL_KEY
        {
            llResetScript();
        }
    }

    listen(integer channel, string name, key id, string MSG)
    {
        text = llParseString2List(llToLower(MSG),[" "],[""]);
        command = llList2String(text ,0); //Format Text
        tempname = llList2String(text ,1);
    
        if(command == "goto" || command == "/goto")
        {
            goto = TRUE;
            integer index = llListFindList(people_in_sensor_range, [llList2String(text ,1)]);
            if(index != -1)
            {
                NAME = llList2String(people_in_sensor_range, index); //if full NAME given
            }
            else if(index == -1)
            {
                integer i = 0;
                integer length = llGetListLength(people_in_sensor_range);
                integer ind;
                do
                {
                    if(llStringLength(llList2String(people_in_sensor_range, i)) > 0)
                    {
                         ind = llSubStringIndex(llList2String(people_in_sensor_range, i), llList2String(text ,1));
                        if(ind != -1)
                        {
                            NAME = llList2String(people_in_sensor_range, i); //if partial NAME given
                            reqid = llHTTPRequest( URL + llEscapeURL(NAME), [], "" );
                            return;
                        }
                    }
                }while(i++<length);
                NAME = llList2String(text ,1) + " " + llList2String(text ,2);
                reqid = llHTTPRequest( URL + llEscapeURL(NAME), [], "" );
            }
        }
        
        else if(command == "beamup" || command == "/beamup")
        {
            goto = FALSE;
            integer index = llListFindList(people_in_sensor_range, [llList2String(text ,1)]);
            if(index != -1)
            {
                NAME = llList2String(people_in_sensor_range, index); //if full NAME given
            }
            else if(index == -1)
            {
                integer i = 0;
                integer length = llGetListLength(people_in_sensor_range);
                integer ind;
                do
                {
                    if(llStringLength(llList2String(people_in_sensor_range, i)) > 0)
                    {
                         ind = llSubStringIndex(llList2String(people_in_sensor_range, i), llList2String(text ,1));
                        if(ind != -1)
                        {
                            NAME = llList2String(people_in_sensor_range, i); //if partial NAME given
                            reqid = llHTTPRequest( URL + llEscapeURL(NAME), [], "" );
                            return;
                        }
                    }
                }while(i++<length);
                NAME = llList2String(text ,1) + " " + llList2String(text ,2);
                reqid = llHTTPRequest( URL + llEscapeURL(NAME), [], "" );
            }
        }
        
        else if(command == "orbit" || command == "/orbit")
        {
            getpos = llGetPos();
            cordx = (integer)getpos.x;
            cordy = (integer)getpos.y;
            cordz = (integer)llList2String(text, 1);
            Pos = <cordx,cordy,cordz>;
            vector myPos = llGetPos();
            rotation myRot = llGetRot();
            vector rezPos = myPos+relativePosOffset*myRot;
            rotation rezRot = relativeRot*myRot;
            llWhisper(-658572700,"New Beam");
            llRezObject(object, rezPos, ZERO_VECTOR, rezRot, startParam);
            llSleep(1.0);
            llWhisper(-658572700,"||warp||"+(string) Pos);
        }
        
        else if(MSG == "random tp" || MSG == "/random tp" )
        {
            cordx = llFrand((256) * 1);
            cordy = llFrand((256) * 1);
            cordz = llFrand((4000) * 1);
            Pos = <cordx,cordy,cordz>;
            vector myPos = llGetPos();
            rotation myRot = llGetRot();
            vector rezPos = myPos+relativePosOffset*myRot;
            rotation rezRot = relativeRot*myRot;
            llWhisper(-658572700,"New Beam");
            llRezObject(object, rezPos, ZERO_VECTOR, rezRot, startParam);
            llSleep(1.0);
            llWhisper(-658572700,"||warp||"+(string) Pos);
        }
        
        else if(MSG == "ground" || MSG == "surface" || MSG == "/ground" || MSG == "/surface")
        {
            getpos = llGetPos();
            cordx = (integer)getpos.x;
            cordy = (integer)getpos.y;
            cordz = (integer)llGround(ZERO_VECTOR) + 1.75;
            Pos = <cordx,cordy,cordz>;
            vector myPos = llGetPos();
            rotation myRot = llGetRot();
            vector rezPos = myPos+relativePosOffset*myRot;
            rotation rezRot = relativeRot*myRot;
            llWhisper(-658572700,"New Beam");
            llRezObject(object, rezPos, ZERO_VECTOR, rezRot, startParam);
            llSleep(1.0);
            llWhisper(-658572700,"||warp||"+(string) Pos);
        } 
        
        else if(MSG == "clear" || MSG == "clear beam")
        {
            llWhisper(-658572700,"ClearBeam||HUD");
        }
        
        else if(MSG == "tp-reboot")
        {
            llOwnerSay("/me Is Rebooting");
            llMessageLinked(LINK_SET, 0, "reset", "");
            llWhisper(-34534534545,(string)llGetOwner() + "||reset");
        }
        else if(MSG == "clear platform")
        {
            llRegionSay(-34534534581,(string)llGetOwner() + "||clearplat");
        }
    }
    
    sensor(integer x)
    {
        integer i = 0;
        --x;
        do
        {
            integer index2 = llListFindList(people_in_sensor_range, [llToLower(llDetectedName(i))]);
            if(llStringLength(llDetectedName(i)) >! 0)
            {
                if(index2 == -1)
                {
                    people_in_sensor_range += llToLower(llDetectedName(i));
                }
            }
        }while(i++<x);
    }
    
    link_message(integer sender_num, integer num, string msg, key id) 
    {
        if( msg == "reset" )
        {
             llResetScript();
        }
    }

    http_response(key id, integer status, list meta, string body)
    {
        if ( id != reqid )
            return;
        if ( status == 499 )
            llOwnerSay("ERROR");
        else if ( status != 200 )
        {
            llOwnerSay("Avatar Not Found");
            return;
        }
        else
            KEY = body;
            if( body == "00000000-0000-0000-0000-000000000000" )
            {
                llOwnerSay("Not Found");
                return;
            }
            list AvPos = llGetObjectDetails(KEY, ([OBJECT_POS]));
            vector AvPos2 = llList2Vector(AvPos,0) + <1.0, 0.0, 0.2>;
            if( AvPos2 == <1.0, 0.0, 0.2> )
            {
                llOwnerSay("Avatar Not In Sim");
                return;
            }
            if( goto == TRUE )
            {
                vector myPos = llGetPos();
                rotation myRot = llGetRot();
                vector rezPos = myPos+relativePosOffset*myRot;
                rotation rezRot = relativeRot*myRot;
                llWhisper(-658572700,"New Beam");
                llRezObject(object, rezPos, ZERO_VECTOR, rezRot, startParam);
                llSleep(1.0);
                llWhisper(-658572700,"||warp||"+(string) AvPos2);
            }
            else if( goto == FALSE )
            {
                vector myPos = llGetPos();
                rotation myRot = llGetRot();
                vector rezPos = myPos+relativePosOffset*myRot;
                rotation rezRot = relativeRot*myRot;
                llWhisper(-658572700,"New Beam");
                llRezObject(object, rezPos, ZERO_VECTOR, rezRot, startParam);
                llSleep(1.0);
                llWhisper(-658572700,"||beamup||"+(string) AvPos2);
            }
    }
}